<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改页</title>
    <link rel="stylesheet" href="./static/css/index.css">
</head>

<body>
    <h1>修改页</h1>
    <div>
        <div>
            <table>
                <tr>
                    <td>
                        姓名: <input id="uname" type="text" placeholder="请输入姓名">
                    </td>
                    <td>
                        年龄: <input id="uage" type="text" placeholder="请输入年龄">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value="确认" onclick="editU()">
                        <input type="button" value="取消" onclick="cancelU()">
                    </td>
                </tr>

            </table>
        </div>
    </div>
    <script src="./static/js/axios.min.js"></script>
    <script src="./static/js/jquery-3.7.1.min.js"></script>
    <script>
        const api = "http://localhost:3000"
        let id = new URLSearchParams(location.search).get('id')
        function getU() {

            console.log(id);
            axios.get(`${api}/user/${id}`).then(res => {
                $('#uname').val(res.data.data.uname)
                $('#uage').val(res.data.data.uage)
            })
        }

        getU()

        function editU() {
            let uname = $('#uname').val()
            let uage = $('#uage').val()

            if (confirm("确认增加用户吗")) {
                axios.put(`${api}/user/${id}`, { uname, uage }).then(() => {
                    location.href = './index.html'
                })
            }
        }

        function cancelU() {
            if (confirm("确认返回首页吗？？？")) {
                location.href = './index.html'
            }
        }
    </script>
</body>

</html>